<?php


namespace App\Http\Controllers\System\Dictionary;


use App\Http\Controllers\Controller;
use App\Http\Requests\System\Dictionary\HsCode\StoreRequest;
use App\Http\Requests\System\Dictionary\HsCode\UpdateRequest;
use App\Http\Searchs\System\Dictionary\HsCodeSearch;
use App\Repositories\HsCode;

class HsCodeController extends Controller
{
    /**
     * @var HsCode
     */
    private $code;

    /**
     * HsCodeController constructor.
     * @param HsCode $code
     */
    public function __construct(HsCode $code)
    {
        $this->code = $code;
    }

    /**
     * @param HsCodeSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(HsCodeSearch $search)
    {
        return $this->response(
            $this->code->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->code->create($request->only([
                'element', 'name', 'code', 'unit1', 'unit2'
            ]))
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id){
        return $this->response(
            $this->code->find($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->code->update($id, $request->only([
                'element', 'name', 'code', 'unit1', 'unit2'
            ]))
        );
    }
}
